# -- coding: utf-8 --
# file: xls.py
#
import sys
import pyperclip as clp
import xlrd
import xlwt
import xlutils
import str_operate
from xlutils.copy import copy


def append_time(dct, str):
    """
    把str里面出现的01~49统计到dct里面

    Args:
        dct ([字典]): [description]
        str ([字符串]): [description]
    """
    s = str.split()
    for item in s:
        if item in dct:
            dct[item] += 1


def find_in_dict_by_pos(dct, pos):
    """
    根据pos按照从下到上，从左到右的顺序在dct中找出对应的值
    该函数会改变参数
    Args:
        dct ([字典]): [description]
        pos ([整数]): [description]

    返回dct其中一个元素的key
    """
    small_dct = dict()
    big_dct = dict()
    cut = dct.popitem()  # 从dct中取出一个item
    # 按cut[1]分成small_dct,big_dct
    for item in dct:
        if cut[1] < dct[item] or cut[1] == dct[item] and item < cut[0]:
            big_dct[item] = dct[item]
        else:
            small_dct[item] = dct[item]

    # 当big_dct元素个数等pos-1时，cut[0]就是目标
    if len(big_dct) == pos - 1:
        return cut[0]
    # 假如big_dct元素个数大于pos，则在big_dct里继续找
    elif len(big_dct) > pos - 1:
        return find_in_dict_by_pos(big_dct, pos)
    # 假如big_dct元素个数小于pos，pos -= len(big_dct) - 1, 在small_dct里继续找
    else:
        return find_in_dict_by_pos(small_dct, pos - len(big_dct) - 1)


def get_in_dict_by_key(dct, key):
    """
    按次数在dct中找出key的排位
    该函数不会改变参数

    Args:
        dct ([字典]): [description]
        key ([字符串]): [description]
    """
    # 得到key在dct中对应的value
    iCmp = dct[key]
    pos = 0
    for item in dct:
        # 假如比iCmp大，或者value相等且 < key，pos + 1
        if dct[item] > iCmp or dct[item] == iCmp and item < key:
            pos += 1
    return pos + 1


def main(file_name):
    constant_list = {'major_cycle': 120, 'lesser_cycle': 80, 'mini_cycle': 18}
    std_list = ['01', '02', '03', '04', '05', '06', '07',
                '08', '09', '10', '11', '12', '13', '14',
                '15', '16', '17', '18', '19', '20', '21',
                '22', '23', '24', '25', '26', '27', '28',
                '29', '30', '31', '32', '33', '34', '35',
                '36', '37', '38', '39', '40', '41', '42',
                '43', '44', '45', '46', '47', '48', '49']

    rb = xlrd.open_workbook(filename=file_name)
    rs = rb.sheet_by_index(1)

    lineNO = 1

    result = ''

    # single_search_NO = [rs.cell(0, 0), rs.cell(0, 20), rs.cell(0, 40)]
    # search_120_NO = [[], [], []]
    for major in range(constant_list['major_cycle']):
        dct1 = dict.fromkeys(std_list, 0)
        dct2 = dict.fromkeys(std_list, 0)
        dct3 = dict.fromkeys(std_list, 0)
        for lesser in range(constant_list['lesser_cycle']):
            for mini in range(constant_list['mini_cycle']):
                append_time(dct1, rs.cell(lineNO, mini + 1).value)
                append_time(dct2, rs.cell(lineNO, mini + 20).value)
                append_time(dct3, rs.cell(lineNO, mini + 39).value)
            lineNO += 1
        # 80行数据统计完，dct1,2,3完成

        pos = int(rs.cell(1 + major, 94).value)
        result += find_in_dict_by_pos(dct1, pos) + '\t\t'

        pos = int(rs.cell(1 + major, 96).value)
        result += find_in_dict_by_pos(dct2, pos) + '\t\t'

        pos = int(rs.cell(1 + major, 98).value)
        result += find_in_dict_by_pos(dct3, pos) + '\t\t'

        result += '\n'

    clp.copy(result)


if __name__ == '__main__':
    if sys.platform.startswith('win'):
        cwd = r'D:/_study/pycode/'
        # cwd = r'E:/ok1/'
    elif sys.platform.startswith('linux'):
        cwd = r'/home/huang/pyRepo/fatools/'
    else:
        pass

    rf = 'A1.xls'
    main(cwd + rf)
